package cn.edu.zut.hhr.service.impl;

import cn.edu.zut.hhr.entity.Comment;
import cn.edu.zut.hhr.entity.User;
import cn.edu.zut.hhr.mapper.CommentMapper;
import cn.edu.zut.hhr.mapper.UserMapper;
import cn.edu.zut.hhr.service.CommentService;
import cn.edu.zut.hhr.utils.UserUtil;
import cn.edu.zut.hhr.vo.CommentVo;
import cn.edu.zut.hhr.vo.RestResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author HHR
 * @since 2021-08-25
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Resource
    CommentMapper commentMapper;

    @Resource
    UserMapper userMapper;

    /**
     * @param aid
     * @return
     */
    @Override
    public RestResponse getCommentByAid(Long aid,Integer page, Integer pageSize,Integer sonPage, Integer sonPageSize) {
        IPage<Comment> iPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Comment> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Comment::getAid,aid)
                //只找一级评论,pid为0意味着没有父评论,因为数据库id从1开始
                .eq(Comment::getPid,0);
        commentMapper.selectPage(iPage,queryWrapper);
        List<Comment> comments = iPage.getRecords();
        List<CommentVo> commentVos = getCommentVoList(comments,sonPage,sonPageSize);
        return RestResponse.success(commentVos);
    }

    /**
     * 通过Id查询指定评论
     * @param pid
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public List<CommentVo> getCommentByPid(Long pid, Integer page, Integer pageSize,Integer sonPage, Integer sonPageSize){
        IPage<Comment> iPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Comment> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Comment::getPid,pid);
        commentMapper.selectPage(iPage,queryWrapper);
        List<Comment> comments = iPage.getRecords();
        List<CommentVo> commentVos = getCommentVoList(comments,sonPage,sonPageSize);
        return commentVos;
    }

    @Override
    public RestResponse addComment(Long aid, String content,Long pid, Long toUid) {
        if(pid == null){
            pid = 0L;
        }
        if(toUid == null){
            toUid = 0L;
        }
        User user = UserUtil.currentUser();
        Comment comment = new Comment();
        comment.setUid(user.getId());
        comment.setContent(content);
        comment.setAid(aid);
        comment.setToUid(toUid);
        comment.setPid(pid);
        comment.setDel(0);
        commentMapper.insert(comment);
        return RestResponse.success(null);
    }

    private List<CommentVo> getCommentVoList(List<Comment> comments,Integer sonPage,Integer sonPageSize) {
        List<CommentVo> commentVos = new ArrayList<>();
        comments.forEach(comment -> {
            CommentVo commentVo = getCommentVo(comment,sonPage,sonPageSize);
            commentVos.add(commentVo);
        });
        return commentVos;
    }

    private CommentVo getCommentVo(Comment comment,Integer sonPage, Integer sonPageSize) {
        CommentVo commentVo = new CommentVo();
        User user = userMapper.selectById(comment.getUid());
        if(user == null){
            user = new User();
            user.setName("该账号已注销");
        }
        if(comment.getToUid() != null && comment.getToUid() != 0){
            User toUser = userMapper.selectById(comment.getToUid());
            if(toUser == null){
                toUser = new User();
                toUser.setName("该账户已注销");
            }
            commentVo.setToUser(toUser);
        }
        commentVo.setUser(user);
        commentVo.setComment(comment);
        if(sonPage == -1 || sonPageSize == -1){
            return commentVo;
        }
        List<CommentVo> sonComments = getCommentByPid(comment.getId(),sonPage,sonPageSize,sonPage,sonPageSize);
        commentVo.setSonComments(sonComments);
        return commentVo;
    }
}
